package com.example.tvrecyclerview.fragment;

import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.tvrecyclerview.R;

/**
 * @author: njb
 * @date: 2020/9/24 0024 1:03
 * @desc:
 */
public class OneFragment extends FragmentMenuBase implements View.OnClickListener, View.OnFocusChangeListener {
    private TextView tvBase, tvDefault, tvOther;
    BaseConfigFragment baseFragment = new BaseConfigFragment();
    DefaultConfigFragment defaultConfigFragment = new DefaultConfigFragment();
    OtherFragment otherFragment = new OtherFragment();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_configuration;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setViews(rootView);
        getFragmentManager().beginTransaction().add(R.id.fragment_container, baseFragment, null).add(R.id.fragment_container, defaultConfigFragment, null).add(R.id.fragment_container, otherFragment, null).commit();

        displayBaseFragment();
    }

    @Override
    public void setCacheViewFromContent(View focusView) {

    }

    @Override
    public void setmCacheViewFromTab(View focusView) {

    }

    private void setViews(View rootView) {
        tvBase = rootView.findViewById(R.id.tv_base);
        tvDefault = rootView.findViewById(R.id.tv_default);
        tvOther = rootView.findViewById(R.id.tv_other);
        tvBase.setOnClickListener(this);
        tvDefault.setOnClickListener(this);
        tvOther.setOnClickListener(this);
        tvBase.setOnFocusChangeListener(this);
        tvDefault.setOnFocusChangeListener(this);
        tvOther.setOnFocusChangeListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_base:
                displayBaseFragment();
                break;
            case R.id.tv_default:
                displayDefaultFragment();
                break;
            case R.id.tv_other:
                displayOtherFragment();
                break;
        }
    }

    private void displayOtherFragment() {
        changeFragment(otherFragment);
    }

    private void displayDefaultFragment() {
        changeFragment(defaultConfigFragment);
    }

    private void displayBaseFragment() {
        changeFragment(baseFragment);
    }

    private void changeFragment(Fragment fragment) {
        if (fragment == baseFragment) {
            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransaction.hide(defaultConfigFragment).hide(otherFragment).show(baseFragment).commit();
        } else if (fragment == defaultConfigFragment) {
            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransaction.hide(baseFragment).hide(otherFragment).show(defaultConfigFragment).commit();
        } else if (fragment == otherFragment) {
            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager()
                    .beginTransaction();
            fragmentTransaction.hide(baseFragment).hide(defaultConfigFragment).show(otherFragment).commit();
        }
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            switch (v.getId()) {
                case R.id.tv_base:
                    displayBaseFragment();
                    break;
                case R.id.tv_default:
                    displayDefaultFragment();
                    break;
                case R.id.tv_other:
                    displayOtherFragment();
                    break;
            }
        }
    }
}
